package com.example.base.config;

import com.example.base.utils.RedisUtil;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, JsonNode> redisTemplate(LettuceConnectionFactory connectionFactory){
        RedisTemplate<String, JsonNode> redisTemplate = new RedisTemplate<>();
        redisTemplate.setKeySerializer(RedisSerializer.string());
        //可以自定义解析类
        redisTemplate.setValueSerializer(RedisSerializer.json());
        redisTemplate.setConnectionFactory(connectionFactory);
        //初始化到工具类中
        RedisUtil.setRedisTemplate(redisTemplate);
        return redisTemplate;
    }
}
